package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.module.common.iap.IapGift;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameSystem;
import com.motu.monstercity.protocol.CsGameSystem.CSGameSystemRedPointResponse;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.ActConstant;
import com.motu.monstercity.server.game.commondata.act.ActTask;
import com.motu.monstercity.server.game.commondata.bag.*;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.system.SystemConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.List;

public class OnceActManager extends OnceActUserData {
    // 初始化，玩家init接口下发一次性固定活动的用户数据
    public static void loadInitUserData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        loadCityLevelRewardAct(pbUserData, userInfo);// 加载都市等级活动数据
        loadUserDailyRaffleDrawData(pbUserData, userInfo);// 加载每日特惠充值抽奖数据
        // 有新的在这边添加
        loadUserDisCountGiftData(pbUserData, userInfo); // 折扣礼包数据
        loadUserDailySupplyData(pbUserData, userInfo); // 每日补给数据
    }


    // 加载都市等级活动数据
    public static void loadCityLevelRewardAct(PBUserData.Builder pbUserData, UserInfo userInfo) {
        long userId = userInfo.getId();
        int taskGroupId = ActConstant.ACT_TASK_GROUP_ID_CITY;
        UserActTaskReward userActTaskReward = OnceActManager.getUserActTastReward(userId, taskGroupId);
        if (userActTaskReward == null) {// 未参与过
            pbUserData.getOnceActBuilder().getCityLevelRewardBuilder().setIsComplete(false);
        } else {// 已经参与过了，看奖励是否已经全部领取了
            if (checkCityLeveReward(taskGroupId, userActTaskReward)) {// 已经全部领取
                pbUserData.getOnceActBuilder().getCityLevelRewardBuilder().setIsComplete(true);
            } else {
                pbUserData.getOnceActBuilder().getCityLevelRewardBuilder().setIsComplete(false);
                ProtoDataUtils.updatePBUserData(pbUserData, userActTaskReward, true);
            }
        }
    }

    // 判断都市等级活动的奖励是否全部领取了
    public static boolean checkCityLeveReward(int taskGroupId, UserActTaskReward userActTaskReward) {
        JsonArray jsonArray = userActTaskReward.getRewardInfoArray();// 领取记录
        for (ActTask actTask : ActDaysManager.getActTaskMap().values()) {
            if (actTask.getTaskGroupId() == taskGroupId && !Tool.isInList(jsonArray, actTask.getNum1())) {
                return false;
            }
        }
        return true;
    }

    // 加载每日特惠充值抽奖数据
    public static void loadUserDailyRaffleDrawData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        UserDailyRaffleDraw userDailyRaffleDraw = ActManager.getUserDailyRaffleDraw(userInfo.getId());
        ProtoDataUtils.updatePBUserData(pbUserData, userDailyRaffleDraw, true);
    }

    // 加载每日特惠充值抽奖数据
    public static void loadUserDailySupplyData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        UserActDailySupply userActDailySupply = ActManager.getUserActDailySupply(userInfo.getId());
        ProtoDataUtils.updatePBUserData(pbUserData, userActDailySupply, true);
    }

    // 加载折扣礼包活动触发数据
    public static void loadUserDisCountGiftData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        UserActDiscountGift discountGift = getDiscountGiftUserData(userInfo.getId());
        if (discountGift.getEndTime() == 0){ // 如果还没触发 尝试触发一下
            SystemManager.tryUnlockSystem(pbUserData,userInfo,SystemConstant.SYSTEM_MODULE_ID_DISCOUNT_GIFT);  //  尝试解锁  TODO 目前是同步响应里塞数据是否异步+推送的形式更好
        }
        // 如果过期了或者没有触发不需要下发数据
        if (TimeUtils.getCurTime() >= discountGift.getEndTime()){
            return;
        }
        // 如果买完了则不需要下发数据
        if (discountGift.getIndex() >= AllParam.DISCOUNT_GIFT_ID.length){
            return;
        }
        ProtoDataUtils.updatePBUserData(pbUserData, discountGift, true);
    }

    /**
     * 系统解锁时-尝试触发用户折扣礼包
     */
    public static UserActDiscountGift tryTriggerDiscountGift(long userId) {
        UserActDiscountGift discountGift = getDiscountGiftUserData(userId);
        if (discountGift.getEndTime() <= 0) {  // 存在值说明之前触发过，反之没有
            discountGift.putEndTime(TimeUtils.getCurTime() + AllParam.DISCOUNT_GIFT_TIME);
            discountGift.update();
        }
        return discountGift;
    }


    /**
     * 检查是否能购买折扣礼包
     * @param userInfo
     * @param iapGift
     * @return
     */
    public static ErrorWord checkBuyGift(UserInfo userInfo, IapGift iapGift) {
        if (iapGift.getType() != IapGift.TYPE_ACT_DISCOUNT_GIFT) {
            return ErrorWord.INVALID_OPERATION;
        }
        // 是否没有解锁
        if (!SystemManager.checkModuleOpen(userInfo, SystemConstant.SYSTEM_MODULE_ID_DISCOUNT_GIFT)) {
            return ErrorWord.MODULE_LOCKED;
        }
        int iapGiftId = iapGift.getId();
        UserActDiscountGift discountGift = getDiscountGiftUserData(userInfo.getId());
        // 超过了有效期或者没有触发数据直接拦截
        if (TimeUtils.getCurTime() > discountGift.getEndTime()) {
            return ErrorWord.NOT_DATA_1;
        }
        long timeDiff = TimeUtils.getTimeDiff(discountGift.getEndTime());
        if (timeDiff >= AllParam.DISCOUNT_GIFT_TIME){
            return ErrorWord.NOT_DATA_10;
        }
        // 判断什么挡位,是否当前可以买的挡位
        int index = discountGift.getIndex();
        if (index >= AllParam.DISCOUNT_GIFT_ID.length){
            return ErrorWord.ALREADY_MAX;
        }
        int giftId =  AllParam.DISCOUNT_GIFT_ID[index];
        if (iapGiftId != giftId){
            return ErrorWord.NOT_DATA_9;
        }
        return null;
    }
}
